package sub3;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import sub1.ClientUI;

import exceptions.FailureConnectionException;
import exceptions.SharedFolderNotFoundException;

/**
 * Frame which enables to change parameters: sharedFolder, address and port of the multicast group.
 * @see ClientUI
 * @author Magali Moulin, Pierre Labiausse
 * @version 3
 */
public class ClientUI_Parameters extends JFrame implements ActionListener {

	private Client client;
	private JButton buttonSave, buttonClose;
	private JTextField txtAddress, txtPort, txtSharedFolderPath;
	private ExecutorService executor = Executors.newFixedThreadPool(1);
	private JLabel lblMcastStatus = new JLabel("");
	private JLabel lblSharedFolderStatus = new JLabel("");
	private String oldSharedFolderPath, oldMcastAddress, oldMcastPort;
	
	/**
	 * Constructor of this class. Create the GUI of parameters frame for the client
	 * @param client reference to the client to be able to connect on the new group.
	 */
	public ClientUI_Parameters (Client client) {
		this.client	 = client;
		this.setBounds(100, 100, 500, 500);
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(4,1));
		this.setContentPane(panel);

		// Add Shared Folder Path parameter
		oldSharedFolderPath = client.getSharedFolderPath();
		JPanel panelSFP = new JPanel();
		JLabel lblSFP = new JLabel("Path of the shared folder : ");
		txtSharedFolderPath = new JTextField(oldSharedFolderPath);
		txtSharedFolderPath.setColumns(30);
		panelSFP.add(lblSFP);
		panelSFP.add(txtSharedFolderPath);
		panelSFP.add(lblSharedFolderStatus);
		panel.add(panelSFP);
		
		// Add Server Address parameter
		oldMcastAddress = client.getMcastAddress().getHostAddress();
		JPanel panelSA = new JPanel();
		JLabel lblSA = new JLabel("Address of the multicast group : ");
		txtAddress = new JTextField(oldMcastAddress);
		txtAddress.setColumns(30);
		panelSA.add(lblSA);
		panelSA.add(txtAddress);
		panelSA.add(lblMcastStatus);
		panel.add(panelSA);
		
		// Add Server Port  parameter
		oldMcastPort = ""+client.getMcastPort();
		JPanel panelSP = new JPanel();
		JLabel lblSP = new JLabel("Port of the multicast group : ");
		txtPort = new JTextField(oldMcastPort);
		txtPort.setColumns(10);
		panelSP.add(lblSP);
		panelSP.add(txtPort);
		panel.add(panelSP);
		
		// Add button to save/cancel parameters
		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
		buttonSave = new JButton("Save");
		buttonSave.addActionListener(this);
		buttonPane.add(buttonSave);
		getRootPane().setDefaultButton(buttonSave);
		buttonClose = new JButton("Close");
		buttonClose.addActionListener(this);
		buttonPane.add(buttonClose);
		panel.add(buttonPane);
		
		this.pack();
		this.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if(arg0.getSource() == buttonSave){
			// If the shared folder path has been changed
			if(! oldSharedFolderPath.equals(txtSharedFolderPath.getText())) {
				System.out.println("Shared Folder Path changed");
				executor.execute(new Runnable() {

					@Override
					public void run() {
						try {
							client.changeSharedFolderPath(txtSharedFolderPath.getText());
							lblSharedFolderStatus.setText("");
						} catch (SharedFolderNotFoundException e) {
							lblSharedFolderStatus.setForeground(Color.RED);
							lblSharedFolderStatus.setText("Shared Folder not found");
						}
					}
				});
			}
			
			
			// If the server address and/or the port have been changed pr if we are not sharing yet
			if(! (oldMcastAddress.equals(txtAddress.getText()) && oldMcastPort.equals(txtPort.getText()) && client.isRegistered()) ){
				System.out.println("Server address changed");
				executor.execute(new Runnable() {
					
					@Override
					public void run() {
						try {
							int portNumber = Integer.parseInt(txtPort.getText());
							InetAddress address = InetAddress.getByName(txtAddress.getText());
							client.share(address, portNumber);
							lblMcastStatus.setText("");
						} catch (FailureConnectionException e) {
							lblMcastStatus.setForeground(Color.RED);
							lblMcastStatus.setText("This is not a multicast address");
						} catch (NumberFormatException f){
							lblMcastStatus.setForeground(Color.RED);
							lblMcastStatus.setText("Port number invalid");
						} catch (UnknownHostException g) {
							lblMcastStatus.setForeground(Color.RED);
							lblMcastStatus.setText("Multicast address invalid");
						}
					}
				});
			}
			
		}
		if(arg0.getSource() == buttonClose){
			dispose();
		}
	}
}
